package net.aiion.weave.impl.web.servlet.http;

import javax.servlet.AsyncContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.aiion.weave.impl.web.service.http.WeaveServiceRunner;

@WebServlet(name = "httpDispatcherServlet", urlPatterns = {
    "/run/*"
}, asyncSupported = true)
public class WeaveServlet extends HttpServlet
{
    private static final long serialVersionUID = 8855276832261065526L;

    public WeaveServlet() {
    }

    @Override
    protected void doPost(final HttpServletRequest aHttpServletRequest, final HttpServletResponse aHttpServletResponse)
    {
        doRequest(aHttpServletRequest, aHttpServletResponse);
    }

    @Override
    protected void doGet(final HttpServletRequest aHttpServletRequest, final HttpServletResponse aHttpServletResponse) {
        doRequest(aHttpServletRequest, aHttpServletResponse);
    }

    protected void doRequest(final HttpServletRequest aHttpServletRequest,
        final HttpServletResponse aHttpServletResponse)
    {
        final AsyncContext asyncContext = aHttpServletRequest.startAsync(aHttpServletRequest, aHttpServletResponse);
        asyncContext.start(new WeaveServiceRunner(asyncContext));
    }
}
